﻿using System.Collections;
using UnityEngine;

/// <summary>
/// 行走逻辑
/// </summary>
public class WalkLogic
{
    private static WalkLogic self;
    private ModelLocator model;

    private float autoCheckDis = 0.8f;
    private float autoFightDis = 0.001f;

    private float arrowShowDis = 1.0f;

    GameObject arrowGuide;

    public static WalkLogic getInstance()
    {
        if(self == null)
        {
            self = new WalkLogic();
        }
        return self;
    }

    public WalkLogic()
    {
        model = ModelLocator.getInstance();
        arrowGuide = arrowGuide==null?GameObject.Find("GO(Panel)"):arrowGuide;
    }

    /// <summary>
    /// 遇怪触发判断
    /// </summary>
    public void encounterMonster(GameObject[] players)
    {
        GameObject role = PlayerData.Instance().MainRoleInstance;
        PlayerAction playAct;
        if (GameData.SceneType == GameDefine.SceneType_CS)
        {
            foreach (GameObject obj in players)
            {
                playAct = obj.GetComponent<PlayerAction>();
                Point p = new Point();
                p.z = ulong.Parse(obj.name);
                if ( obj.activeSelf && !model.scene.isRole(p) )
                {
                    if (Vector2Distance(role.transform.position, obj.transform.position) >= arrowShowDis)
                    {
                        arrowGuide.SetActive(true);
                    }
                    else
                    {
                        arrowGuide.SetActive(false);
                    }

                    if (Vector2Distance(role.transform.position, obj.transform.position) <= autoCheckDis)
                    {
                        playAct.StopWalkAround();
                        playAct.npcautoMove(role.transform.position);
                    }
                    else
                    {
                        playAct.StartWalkAround();
                    }

                    if (Vector2Distance(role.transform.position, obj.transform.position) <= autoFightDis)
                    {
                        //发送遇怪消息//
                    }
                }
            }
        }
    }

    public float Vector2Distance(Vector3 startPoint, Vector3 endPoint)
    {
        Vector2 position1 = new Vector2(startPoint.x, startPoint.y);
        Vector2 position2 = new Vector2(endPoint.x, endPoint.y);

        return Vector2.Distance(position1, position2);
    }



}